package com.hzh.page.common.interceptor;

import com.hzh.page.common.exception.BusinessException;
import com.hzh.page.common.exception.ResponseEnum;
import com.hzh.page.common.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (token == null) {
            throw new BusinessException(ResponseEnum.LOGIN_AUTH_ERROR);
        } else {
            boolean b = JwtUtils.checkToken(token);
            if (b) {
                return b;
            } else {
                throw new BusinessException(ResponseEnum.LOGIN_EXPIRED_ERROR);
            }
        }
    }

}
